home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 April / SGI IRIX 6.5 Applications 2004 April.iso / dist / mozilla.idb / var / netscape / mozilla / chrome / toolkit.jar.z / toolkit.jar / content / global / finddialog.js < prev    next >
Text File  |  2003-11-11  |  6KB  |  155 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code, released March
  14.  * 31, 1998.
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape Communications
  17.  * Corporation. Portions created by Netscape are
  18.  * Copyright (C) 1998 Netscape Communications Corporation. All
  19.  * Rights Reserved.
  20.  *
  21.  * Contributor(s): Alec Flett       <alecf@netscape.com>
  22.  *                 Bill Law         <law@netscape.com>
  23.  *                 Blake Ross       <blakeross@telocity.com>
  24.  *                 Dean Tessman     <dean_tessman@hotmail.com>
  25.  *                 Matt Fisher      <matt@netscape.com>
  26.  *                 Simon Fraser     <sfraser@netscape.com>
  27.  *                 Stuart Parmenter <pavlov@netscape.com>
  28.  */
  29.  
  30. var dialog;     // Quick access to document/form elements.
  31. var gFindInst;   // nsIWebBrowserFind that we're going to use
  32. var gFindInstData; // use this to update the find inst data
  33.  
  34. function initDialogObject()
  35. {
  36.   // Create dialog object and initialize.
  37.   dialog = new Object;
  38.   dialog.findKey         = document.getElementById("dialog.findKey");
  39.   dialog.caseSensitive   = document.getElementById("dialog.caseSensitive");
  40.   dialog.wrap            = document.getElementById("dialog.wrap");
  41.   dialog.searchBackwards = document.getElementById("dialog.searchBackwards");
  42.   dialog.find            = document.getElementById("findDialog").getButton("accept");
  43.   dialog.bundle          = null;
  44.  
  45.   // Move dialog to center, if it not been shown before
  46.   var windowElement = document.getElementById("findDialog");
  47.   if (!windowElement.hasAttribute("screenX") || !windowElement.hasAttribute("screenY"))
  48.   {
  49.     sizeToContent();
  50.     moveToAlertPosition();
  51.   }
  52. }
  53.  
  54. function fillDialog()
  55. {
  56.   // get the find service, which stores global find state
  57.   var findService = Components.classes["@mozilla.org/find/find_service;1"]
  58.                          .getService(Components.interfaces.nsIFindService);
  59.   
  60.   // Set initial dialog field contents. Use the gFindInst attributes first,
  61.   // this is necessary for window.find()
  62.   dialog.findKey.value           = gFindInst.searchString ? gFindInst.searchString : findService.searchString;
  63.   dialog.caseSensitive.checked   = gFindInst.matchCase ? gFindInst.matchCase : findService.matchCase;
  64.   dialog.wrap.checked            = gFindInst.wrapFind ? gFindInst.wrapFind : findService.wrapFind;
  65.   dialog.searchBackwards.checked = gFindInst.findBackwards ? gFindInst.findBackwards : findService.findBackwards;
  66. }
  67.  
  68. function saveFindData()
  69. {
  70.   // get the find service, which stores global find state
  71.   var findService = Components.classes["@mozilla.org/find/find_service;1"]
  72.                          .getService(Components.interfaces.nsIFindService);
  73.  
  74.   // Set data attributes per user input.
  75.   findService.searchString  = dialog.findKey.value;
  76.   findService.matchCase     = dialog.caseSensitive.checked;
  77.   findService.wrapFind      = dialog.wrap.checked;
  78.   findService.findBackwards = dialog.searchBackwards.checked;
  79. }
  80.  
  81. function onLoad()
  82. {
  83.   initDialogObject();
  84.  
  85.   // Change "OK" to "Find".
  86.   dialog.find.label = document.getElementById("fBLT").getAttribute("label");
  87.   dialog.find.accessKey = document.getElementById("fBLT").getAttribute("accesskey");
  88.  
  89.   // get the find instance
  90.   var arg0 = window.arguments[0];
  91.   if (arg0 instanceof window.opener.nsFindInstData) {
  92.     gFindInstData = arg0;
  93.     gFindInst = gFindInstData.webBrowserFind;
  94.   } else {
  95.     // If the dialog was opened from window.find(), findInst will be an
  96.     // nsISupports interface, so QueryInterface anyway to nsIWebBrowserFind.
  97.     gFindInst = arg0.QueryInterface(Components.interfaces.nsIWebBrowserFind);
  98.   }
  99.  
  100.   fillDialog();
  101.   doEnabling();
  102.  
  103.   if (dialog.findKey.value)
  104.     dialog.findKey.select();
  105.   dialog.findKey.focus();
  106. }
  107.  
  108. function onUnload()
  109. {
  110.     window.opener.findDialog = 0;
  111. }
  112.  
  113. function onAccept()
  114. {
  115.   if (gFindInstData && gFindInst != gFindInstData.webBrowserFind) {
  116.     gFindInstData.init();
  117.     gFindInst = gFindInstData.webBrowserFind;
  118.   }
  119.  
  120.   // Transfer dialog contents to the find service.
  121.   saveFindData();
  122.  
  123.   // set up the find instance
  124.   gFindInst.searchString  = dialog.findKey.value;
  125.   gFindInst.matchCase     = dialog.caseSensitive.checked;
  126.   gFindInst.wrapFind      = dialog.wrap.checked;
  127.   gFindInst.findBackwards = dialog.searchBackwards.checked;
  128.   
  129.   // Search.
  130.   var result = gFindInst.findNext();
  131.  
  132.   if (!result)
  133.   {
  134.     if (!dialog.bundle)
  135.       dialog.bundle = document.getElementById("findBundle");
  136.  
  137.     try {
  138.       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
  139.       promptService = promptService.QueryInterface(Components.interfaces.nsIPromptService);
  140.       promptService.alert(window, null,
  141.                           dialog.bundle.getString("notFoundWarning"));
  142.     }
  143.     catch (e) { dump("The text you entered was not found."); }
  144.  
  145.     dialog.findKey.select();
  146.     dialog.findKey.focus();
  147.   } 
  148.   return false;
  149. }
  150.  
  151. function doEnabling()
  152. {
  153.   dialog.find.disabled = !dialog.findKey.value;
  154. }
  155.